require 'test/unit'
require 'watir'

         #~ $un="crux@eforceglobal.com"
         #~ $pwd="wafer"
         #~ $ip="10.7.10.178"
         #~ #$ie = Watir::IE.start('http://'+$ip+'/cruxWebUI/CruxHome.aspx')
         #~ $filepath="E:/Crux_Automation"
         
    class TC_04Locations < Test::Unit::TestCase  
 
  #~ def test_01Login
  
         #~ require 'watir'

        #~ puts "User is going to login to the Crux Application"
        #~ sleep 5
        #~ $ie.text_field(:name,"ctl00$ContentPlaceHolder1$txtUserName$txtWaterMarkedTextBox").set $un
        #~ $ie.text_field(:name,"ctl00$ContentPlaceHolder1$txtPassword$txtWaterMarkedTextBox").set $pwd
        #~ $ie.button(:name,"ctl00$ContentPlaceHolder1$btnLogin").click
        #~ puts "Successfully logged into the Application"
        #~ sleep 10

    #~ end

  
def test_02AddLocation
  
require 'xls'
require 'watir'

xlFile = XLS.new($filepath + "/Crux_Data/Add_Location.xls") #grab the data file in the same dirrectory
myData = xlFile.getRowRecords('','Sheet1')  #pull data records  from excel
xlFile.close

puts "**************************"
puts "Add Location Functionality"
puts "**************************"

myData.each do |record|
  
  $ie.link(:text,"Administration").click
  sleep 5

  $ie.link(:text,"Manage Companies").click
   sleep 5
  
  puts "Verifying existing Company : '#{record['CompanyName']}' for creating Location"   
 
 b=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies").row_count
   
     #puts b
     
     for i in 2..b do
   a=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies")[i][2].text 
    #puts a
    if a=="#{record['CompanyName']}"      
     puts "Company: '#{record['CompanyName']}' verified successfully"   
    sleep 5
     puts "Creating a Location '#{record['LName']}'"
    $ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies")[i][7].button(:src,"http://"+$ip+"/CruxWebUI/appimages/manage_company.gif").click
    sleep 5
    $ie.link(:id,"ctl00_ContentPlaceHolder1_lnkLocations").click    
    sleep 8
    $ie.button(:name,"ctl00$ContentPlaceHolder1$btnAddNewLocation").click
    sleep 5
    
    $ie.text_field(:id, 'ctl00_ContentPlaceHolder1_txtLocationName_txtWaterMarkedTextBox').set(record['LName'])
     $ie.text_field(:id, 'ctl00_ContentPlaceHolder1_txtLocationAddress_txtWaterMarkedTextBox').set(record['LAddress'])
    $ie.text_field(:id, 'ctl00_ContentPlaceHolder1_txtLocationComments_txtWaterMarkedTextBox').set(record['LComments']) 
    
    $ie.button(:id,"ctl00_ContentPlaceHolder1_btnSave").click
    sleep 5   
 puts "Location: '#{record['LName']}'created Successfully "
 
 puts "Verifying the Location: '#{record['LName']}'"
 
 c=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdLocations").row_count
   
     #puts c
     
     for j in 2..c do
   u=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdLocations")[j][2].text 
    #puts u    
    if u=="#{record['LName']}"      
      puts "Location :'#{record['LName']}' verified successfully"
      break
    end
    end  
      break  
  end   
end
end

end

def test_03EditLocation

require 'xls'
require 'watir'

xlFile = XLS.new($filepath + "/Crux_Data/Edit_Location.xls") #grab the data file in the same dirrectory
myData = xlFile.getRowRecords('','Sheet1')  #pull data records  from excel
xlFile.close

puts "**************************"
puts "Edit Location Functionality"
puts "**************************"

myData.each do |record|
  
  $ie.link(:text,"Administration").click
  sleep 5

  $ie.link(:text,"Manage Companies").click
   sleep 5
  
   puts "Verifying existing Company : '#{record['CompanyName']}' for Location details"   
 
   b=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies").row_count
   
     #puts b
     
     for i in 2..b do
   a=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies")[i][2].text 
    #puts a
    if a=="#{record['CompanyName']}"      
     puts "Company: '#{record['CompanyName']}' verified successfully"   
    sleep 5
     
    $ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies")[i][7].button(:src,"http://"+$ip+"/CruxWebUI/appimages/manage_company.gif").click
    sleep 5
    $ie.link(:id,"ctl00_ContentPlaceHolder1_lnkLocations").click    
    sleep 8
  
    puts "Verifying Location : '#{record['LName']}'for update"
    c=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdLocations").row_count
     for j in 2..c do
   u=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdLocations")[j][2].text 
    #puts u    
    if u=="#{record['LName']}"      
      puts "Location :'#{record['LName']}' verified successfully"
      $ie.table(:id,"ctl00_ContentPlaceHolder1_grdLocations")[j][5].button(:src,"http://"+$ip+"/CruxWebUI/appimages/edit_location_details.gif").click  
      sleep 5
      puts "Updating the Location: '#{record['LName']}' as '#{record['ELName']}'"
      $ie.text_field(:id, 'ctl00_ContentPlaceHolder1_txtLocationName_txtWaterMarkedTextBox').set(record['ELName'])
     $ie.text_field(:id, 'ctl00_ContentPlaceHolder1_txtLocationAddress_txtWaterMarkedTextBox').set(record['ELAddress'])
    $ie.text_field(:id, 'ctl00_ContentPlaceHolder1_txtLocationComments_txtWaterMarkedTextBox').set(record['ELComments']) 
    
    $ie.button(:id,"ctl00_ContentPlaceHolder1_btnSave").click
    sleep 5   
      puts "Location: '#{record['LName']}' successfully updated as '#{record['ELName']}'"
 
      puts "Verifying the updated Location: '#{record['ELName']}'"
 
      rc=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdLocations").row_count
   
     #puts c
     
     for k in 2..rc do
      eu=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdLocations")[k][2].text 
    #puts eu  
    if eu=="#{record['ELName']}"      
      puts "Updated Location :'#{record['ELName']}' verified successfully"
      break
    end
    end      
      break
    end
    end
    break
  end 
end
end   
 
end

def test_04DeleteLocation

require 'xls'
require 'watir'
require 'watir/ie' 
require 'watir/contrib/enabled_popup'

xlFile = XLS.new($filepath + "/Crux_Data/Delete_Location.xls") #grab the data file in the same dirrectory
myData = xlFile.getRowRecords('','Sheet1')  #pull data records  from excel
xlFile.close

puts "***************************"
puts "Delete Location Functionality"
puts "***************************"

myData.each do |record|
  
  $ie.link(:text,"Administration").click
  sleep 5

  $ie.link(:text,"Manage Companies").click
   sleep 5
  
   puts "Verify a Company : '#{record['CompanyName']}' for deleting Location"   
 
   b=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies").row_count
   
     #puts b
     
     for i in 2..b do
   a=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies")[i][2].text 
    #puts a
    if a=="#{record['CompanyName']}"      
     puts "Company: '#{record['CompanyName']}' verified successfully"   
    sleep 5
     
    $ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies")[i][7].button(:src,"http://"+$ip+"/CruxWebUI/appimages/manage_company.gif").click
    sleep 5
    $ie.link(:id,"ctl00_ContentPlaceHolder1_lnkLocations").click    
    sleep 8
  
    puts "Verifying Location : '#{record['LName']}'for delete"
    c=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdLocations").row_count
     for j in 2..c do
   u=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdLocations")[j][2].text 
    #puts u    
    if u=="#{record['LName']}"      
      puts "Location :'#{record['LName']}' verified successfully"
      
      puts "Deleting the Location: '#{record['LName']}'"
      
      $ie.table(:id,"ctl00_ContentPlaceHolder1_grdLocations")[j][5].button(:src,"http://"+$ip+"/CruxWebUI/appImages/delete.gif").click_no_wait
      sleep 5
      
      #--------------------------------------------------------------------------------------------
      
       hwnd = $ie.enabled_popup(5)

       if (hwnd)  #yeah! a popup
       popup = WinClicker.new
       popup.makeWindowActive(hwnd)
       popup.clickWindowsButton("Microsoft Internet Explorer", "OK", 30)
       end
 
       sleep 8      
  
      #-----------------------------------------------------------------------------------------------
 
      puts "Location: '#{record['LName']}'deleted  successfully "
 
      puts "Verifying the deleted Location: '#{record['LName']}'"
 
      rc=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdLocations").row_count
   
     #puts c
     
     for k in 2..rc do
      eu=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdLocations")[k][2].text 
    #puts eu  
    if eu=="#{record['LName']}"      
      flag=1
      break
    end
  end   
if flag==1
puts "Location '#{record['LName']}' not deleted successfully"
else
  puts "Deleted Location '#{record['LName']}' verified successfully"
  end
      break
    end
    end
    break
  end 
end
end   
 
 end


  #~ def test_05Logout
            #~ puts "Logging out from the Application"
            #~ $ie.link(:id,"ctl00_Logout_lnkLogout").click
            #~ puts "Successfully logged out from the Application"
            #~ puts "********************************"
            #~ puts "End of Locations Functionality"
            #~ puts "********************************"
            #~ sleep 5
            #~ $ie.close
  #~ end
  
  end
